<template>
	<view>
		<div class="container padding_b_0">
			<div class="dflex_wrap">
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">动态标题：</div>
					<el-input v-model.trim="req.title" class="search_input" placeholder="请输入"></el-input>
				</div>
				<el-button size="mini" class="search_btn margin_b_20 margin_r_40" @click="loadData">搜索</el-button>
			</div>
		</div>
		<div class="container use-table">
			<div class="dflex_sb margin_b_15">
				<el-button class="add_btn pos_r padding0" icon="iconfont iconxinzeng" @click="toAdd">新增</el-button>
			</div>
			<el-table :data="tableDatas" highlight-current-row>
				<el-table-column property="_id" label="动态ID" align="center"></el-table-column>
				<el-table-column property="title" label="动态标题" align="center"></el-table-column>
				<el-table-column label="动态主图" align="center">
					<template slot-scope="{ row }">
						<el-image style="width: 100px; height: 80px" :src="row.imgs[0]" :preview-src-list="row.imgs" fit="contain"></el-image>
					</template>
				</el-table-column>
				<el-table-column property="create_time" label="创建时间" align="center">
					<template slot-scope="{ row }">
						<view>{{ new Date(row.create_time).format() }}</view>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" fixed="right" width="120">
					<template slot-scope="{ row }">
						<el-tooltip content="编辑动态" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-button icon="iconfont iconbianji" circle @click="toEdit(row)"></el-button>
						</el-tooltip>
						<el-tooltip content="删除动态" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-button icon="iconfont iconshanchu" circle @click="deleDynamics(row)"></el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				:current-page="req.page"
				:page-sizes="[10, 20, 30, 50, 100]"
				:page-size="req.rows"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableTotal"
				@size-change="sizeChange"
				@current-change="currentChange"
			></el-pagination>
		</div>
	</view>
</template>

<script>
export default {
	data() {
		return {
			req: {
				page: 1,
				rows: 20,
				title: ''
			},
			tblHeight: 0,
			tableDatas: [],
			tableTotal: 0
		};
	},
	onLoad() {
		this.loadData();
	},
	methods: {
		async loadData() {
			let res = await this.$func.useadmin.call('dynamics/getDynamicsList', {
				page: this.req.page,
				pageSize: this.req.rows,
				title: this.req.title
			});
			console.log(res, '8848');
			if (res.code == 200) {
				this.tableDatas = res.datas.data;
				this.tableTotal = res.datas.total;
			}
		},
		sizeChange(size) {
			this.req.rows = size;
			this.loadData();
		},
		currentChange(current) {
			this.req.page = current;
			this.loadData();
		},
		toAdd() {
			uni.navigateTo({
				url: `/pages/dynamics/Dynamics_add_edit?tab=新增动态`,
				events: {
					refreshData: () => {
						this.loadData();
					}
				}
			});
		},
		toEdit(row) {
			uni.navigateTo({
				url: `/pages/dynamics/Dynamics_add_edit?id=${row._id}&tab=编辑动态`,
				events: {
					refreshData: () => {
						this.loadData();
					}
				}
			});
		},
		deleDynamics(row) {
			const _this = this;
			uni.showModal({
				title: '提示',
				content: '确认删除？',
				confirmColor: '#e87270',
				success: async function (res) {
					if (res.confirm) {
						let res = await _this.$func.useadmin.call('dynamics/deleteDynamics', {
							id: row._id
						});
						if (res.code == 200) {
							_this.$message.success('删除成功');
							_this.loadData();
						}
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
	},
};
</script>

<style lang="scss"></style>
